package com.calendar.backend;

import java.util.Vector;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.util.Log;

public class Event extends Activity {

	private Context context = null;
	private String eventID = "";
	private String calID = "";
	private String title = "";
	private String description = "";
	private String location = "";
	private long startTime = 0;	//	In milliseconds
	private long endTime = 0;	//	In milliseconds
	private boolean allDay = false;
	private boolean alarm = false;
	private int color = 0;
	public Vector<Event> concurrentEvents = null; 
	public int concurrentPos = 0;
	
	private Uri eventsUri = Uri.parse("content://com.android.calendar/events");
	
	/**
	 * Initializes the object.
	 * 
	 * @param addEvent		True if this is a new event, false if it already exists in Google Calendar.
	 * @param context		The context of the calling object.
	 * @param eventID		The id of the event.
	 * @param calID			The calendar to add the event to.
	 * @param title			The title of the event.
	 * @param description	The description for the event.
	 * @param location		The location of the event.
	 * @param startTime		The start time of the event, in milliseconds.
	 * @param endTime		The end time of the event, in milliseconds.
	 * @param allDay			True if this is an all day event.
	 * @param alarm			True if an alamr should go off.
	 * @param color			Color of the event.
	 */
	public Event(boolean addEvent, Context context, String eventID, String calID, String title, String description, String location,
			long startTime, long endTime, boolean allDay, boolean alarm, int color) {
		
		//	Save values.
		this.context = context;
		this.eventID = eventID;
		this.calID = calID;
		this.title = title;
		this.description = description;
		this.location = location;
		this.startTime = startTime;
		this.endTime = endTime;
		this.allDay = allDay;
		this.alarm = alarm;
		this.color = color;
		
		if(addEvent)
			addEvent();
	}

	
	/**
	 * Add this event to the Andriod's calendar.
	 */
	private void addEvent() {
		
		ContentValues event = new ContentValues();
		String temp = "";
		
		//	Indicate which calendar to add this event to.
		event.put("calendar_id", calID);
		
		//	Add the event's info.
		event.put("title", title);
		event.put("description", description);
		event.put("eventLocation", location);
		event.put("dtstart", "" + startTime);
		event.put("dtend", "" + endTime);
		
		if(allDay)
			temp = "1";
		
		else
			temp = "0";
			
		event.put("allDay", temp);
		
		if(alarm)
			temp = "1";
	
		else
			temp = "0";
		
		event.put("hasAlarm",temp);
		
		//	Add the event to the calendar.
		context.getContentResolver().insert(eventsUri, event);
	}
	
	/**
	 * Remove this event from the Android's calendar.
	 */
	public void deleteEvent() {
		
		context.getContentResolver().delete(eventsUri,
				"calendar_id=? and title=? and description=? and dtstart=? and dtend=?",
				new String[]{calID, 
					title, 
					description, 
					String.valueOf(startTime), 
					String.valueOf(endTime)});
	}
	
	public String getEventID() {
		
		return eventID;
	}
	
	public String getCalID() {
		
		return calID;
	}
	
	public String getEventTitle() {
		
		return title;
	}
	
	public String getDescription() {
		
		return description;
	}
	
	public String getLocation() {
		
		return location;
	}
	
	public long getStartTime() {
		
		return startTime;
	}
	
	public long getEndTime() {
		
		return endTime;
	}
	
	public long getLength() {
		
		return endTime - startTime;
	}
	
	public boolean getAllDay() {
		
		return allDay;
	}
	
	public boolean getAlarm() {
		
		return alarm;
	}
	
	public int getColor() {
		
		return color;
	}
	
	public void setCalID(String calID) {
		
		deleteEvent();
		this.calID = calID;
		addEvent();
	}
	
	public void setEventTitle(String title) {

		deleteEvent();
		this.title = title;
		addEvent();
	}
	
	public void setDescription(String description) {
		
		deleteEvent();
		this.description = description;
		addEvent();
	}
	
	public void setLocation(String location) {

		deleteEvent();
		this.location = location;
		addEvent();
	}
	
	public void setStartTime(long startTime) {

		deleteEvent();
		this.startTime = startTime;
		addEvent();
	}
	
	public void setEndTime(long endTime) {

		deleteEvent();
		this.endTime = endTime;
		addEvent();
	}
	
	public void setAllDay(boolean allDay) {
	
		deleteEvent();
		this.allDay = allDay;
		addEvent();
	}
	
	public void setAlarm(boolean alarm) {
		
		deleteEvent();
		this.alarm = alarm;
		addEvent();
	}
}
